/**
 * 扩展jquery的基本功能
 * 
 */
(function($){
	/**
	 * 回车键事件 $(selector).enter(function(){});
	 */
	$.fn.enter = function(fn){
		return this.each(function(){
			var $this = $(this);
			$this.keyup(function(event){
				if(event.which == 13){
					fn();
				}
			});
		});
	};
	
	/**
	 * delay hover event callback
	 */
	$.fn.delayhover = function(delay, enter){
		return this.each(function(){
			var $this = $(this);
			var interval = null;			
			$this.mouseenter(function(event){
				interval = setTimeout(function(){
					enter(event);
				}, delay);
			}).mouseleave(function(event){
				if(interval){
					clearTimeout(interval);
				}
			});
		});
	};
	
	/** 
	 * set cookie: $.cookie('name', 'foo', {raw: false, expires: 10s, path: '/', domain: '', secure: false});
	 * delele cookie: $.cookie('name', null, {options}); 
	 * get cookie: $.cookie('name', {options});
	 * {options}可选, 如果要操作同一个cookie, {path, domain}必须一样
	 */
	$.cookie = function(key, value, options){				
		//set cookie
		if(arguments.length > 1 && String(value) !== "[object Object]"){
			//set path to default '/'
			options = $.extend({path: '/'}, options);
			if (value === null || value === undefined) {
				//delete cookie
	            options.expires = -1;
	        }				
			
			if(typeof options.expires === 'number'){
				var s = options.expires, t = options.expires = new Date();
				t.setTime(t.getTime() + s * 1000);
			}					
			
			value = String(value);				
			document.cookie = [
			     encodeURIComponent(key), '=',
                 options.raw ? value : encodeURIComponent(value),
                 options.expires ? '; expires=' + options.expires.toUTCString() : '', 
                 options.path ? '; path=' + options.path : '',
                 options.domain ? '; domain=' + options.domain : '',
                 options.secure ? '; secure' : ''
            ].join('');			
		}else{
			//get cookie
			options = value || {};
			var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
		    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
		}	
	};
	
	/**
	 * 滚动条到了最底端调用callback方法
	 */
	$.scroll = function(callback){		
		if($.isFunction(callback)){
			$(window).scroll(function(){
				var $doc = $(document);
				var top = $doc.scrollTop();
				var dh = $doc.height();
				var wh = $(window).height();
				if((dh - top - wh) == 0){
					callback();
				}			
			});
		}		
	};
	
	$.fn.hoverDelay = function(options){
        var defaults = {
            hoverDuring: 600,
            outDuring: 100,
            hoverEvent: function(){
                $.noop();
            },
            outEvent: function(){
                $.noop();    
            }
        };
        var sets = $.extend(defaults,options || {});
        var hoverTimer=null, outTimer=null, that = this;
        return $(this).each(function(){
            $(this).hover(function(){
                clearTimeout(outTimer);
                hoverTimer = setTimeout(function(){sets.hoverEvent.apply(that);}, sets.hoverDuring);
            },function(){
                clearTimeout(hoverTimer);
                outTimer = setTimeout(function(){sets.outEvent.apply(that);}, sets.outDuring);
            });    
        });
    };      
	
})(jQuery);